package cn.stylefeng.guns.core.tag;

import org.beetl.core.*;
import org.springframework.stereotype.*;
import org.springframework.context.annotation.*;
import cn.stylefeng.guns.modular.system.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.util.*;
import cn.stylefeng.guns.core.common.exception.*;
import cn.stylefeng.roses.kernel.model.exception.*;
import org.apache.commons.lang3.*;
import java.io.*;
import java.util.*;
import cn.stylefeng.guns.modular.system.entity.*;

@Component
@Scope("prototype")
public class DictSelectorTag extends Tag
{
    @Autowired
    DictService iDictService;

    public void render() {
        final Map attrs = (Map)this.args[1];
        if (ToolUtil.isEmpty(attrs.get("code"))) {
            throw new ServiceException((AbstractBaseExceptionEnum)BizExceptionEnum.ERROR_CODE_EMPTY);
        }
        final String code = attrs.get("code").toString();
        final String type = ToolUtil.isNotEmpty(attrs.get("type")) ? attrs.get("type").toString() : "select";
        final String multiple = ToolUtil.isNotEmpty(attrs.get("multiple")) ? attrs.get("multiple").toString() : "";
        final String label = ToolUtil.isNotEmpty(attrs.get("label")) ? attrs.get("label").toString() : "";
        final String placeholder = ToolUtil.isNotEmpty(attrs.get("placeholder")) ? attrs.get("placeholder").toString() : "";
        final String width = ToolUtil.isNotEmpty(attrs.get("width")) ? attrs.get("width").toString() : "248";
        final String value = ToolUtil.isNotEmpty(attrs.get("value")) ? attrs.get("value").toString() : "";
        final String id = ToolUtil.isNotEmpty(attrs.get("id")) ? attrs.get("id").toString() : "";
        final String name = ToolUtil.isNotEmpty(attrs.get("name")) ? attrs.get("name").toString() : "";
        final String underline = ToolUtil.isNotEmpty(attrs.get("underline")) ? attrs.get("underline").toString() : "";
        final String onchange = ToolUtil.isNotEmpty(attrs.get("onchange")) ? attrs.get("onchange").toString() : "";
        final String readonly = ToolUtil.isNotEmpty(attrs.get("readonly")) ? attrs.get("readonly").toString() : "";
        final String disabled = ToolUtil.isNotEmpty(attrs.get("disabled")) ? attrs.get("disabled").toString() : "";
        final String theme = ToolUtil.isNotEmpty(attrs.get("theme")) ? "simple" : "";
        final String layFilter = ToolUtil.isNotEmpty(attrs.get("layFilter")) ? attrs.get("layFilter").toString() : "";
        final int searchnum = ToolUtil.isNum(attrs.get("searchnum")) ? Integer.parseInt(attrs.get("searchnum").toString()) : 10;
        final List<Dict> list = (List<Dict>)this.iDictService.selectByTypeCode(code);
        StringBuffer html = new StringBuffer();
        if (StringUtils.isBlank((CharSequence)theme)) {
            html.append("<div class=\"layui-form-item\">\r\n");
            html.append("<label class=\"layui-form-label\">" + label + "</label>\r\n");
            html.append("<div class=\"layui-input-block\">\r\n");
        }
        if ("radio".equals(type)) {
            list.forEach(obj -> {
                html.append("<input type=\"radio\" ");
                if ("true".equals(disabled) || "disabled".equals(disabled)) {
                    html.append("disabled ");
                }
                else if (ToolUtil.isNotEmpty(name)) {
                    html.append("name=\"" + name + "\" ");
                }
                if ("true".equals(readonly) || "disabled".equals(readonly)) {
                    html.append("disabled ");
                }
                if (ToolUtil.isNotEmpty(value) && value.equals(obj.getDictId())) {
                    html.append("checked ");
                }
                html.append("value=\"" + obj.getDictId() + "\" title='" + obj.getName() + "'>\r\n");
            });
        }
        else if ("checkbox".equals(type)) {
            list.forEach(obj -> {
                html.append("<input type=\"checkbox\" ");
                if ("true".equals(disabled) || "disabled".equals(disabled)) {
                    html.append("disabled ");
                }
                else if (ToolUtil.isNotEmpty(name)) {
                    html.append("name=\"" + name + "\" ");
                }
                if ("true".equals(readonly) || "disabled".equals(readonly)) {
                    html.append("disabled ");
                }
                if (ToolUtil.isNotEmpty(value) && value.equals(obj.getDictId())) {
                    html.append("checked ");
                }
                html.append("value=\"" + obj.getDictId() + "\" title='" + obj.getName() + "'>\r\n");
            });
        }
        else {
            if ("true".equals(multiple)) {
                if (list.size() >= searchnum) {
                    html.append("<select multiple ");
                }
                else {
                    html.append("<select multiple=\"multiple\" size=\"10\" ");
                }
            }
            else {
                html.append("<select ");
            }
            if (ToolUtil.isNotEmpty((Object)layFilter)) {
                html.append(" lay-filter='" + layFilter + "' ");
            }
            if (ToolUtil.isNotEmpty((Object)placeholder)) {
                html.append(" data-placeholder=\"" + placeholder + "\" ");
            }
            if ("true".equals(disabled) || "disabled".equals(disabled)) {
                html.append("disabled=\"disabled\" ");
            }
            else {
                if (ToolUtil.isNotEmpty((Object)id)) {
                    html.append("id=\"" + id + "\" ");
                }
                if (ToolUtil.isNotEmpty((Object)name)) {
                    html.append("name=\"" + name + "\" ");
                }
            }
            if (list.size() >= searchnum) {
                html.append("class=\"form-control chosen-select\" style=\"width:" + width + "px\"  tabindex=\"1\" \r\n");
            }
            else {
                html.append("class=\"form-control\" style=\"width:" + width + "px\" \r\n");
            }
            if ("true".equals(readonly) || "readonly".equals(readonly)) {
                if (list.size() >= searchnum) {
                    html.append("disabled=\"disabled\" ");
                }
                else {
                    html.append("onfocus=\"this.defaultIndex=this.selectedIndex;\" onchange=\"this.selectedIndex=this.defaultIndex;\" ");
                }
            }
            if (ToolUtil.isNotEmpty((Object)onchange)) {
                html.append("onchange=\"" + onchange + "($(this).children('option:selected').val())\" ");
            }
            html.append(">");
            if (ToolUtil.isNotEmpty((Object)placeholder)) {
                html.append("<option value=\"\">" + placeholder + "</option>\r\n");
            }
            list.forEach(obj -> {
                if (ToolUtil.isNotEmpty(value) && value.equals(obj.getDictId())) {
                    html.append("<option selected value=\"" + obj.getDictId() + "\">" + obj.getName() + "</option>\r\n");
                }
                else {
                    html.append("<option value=\"" + obj.getDictId() + "\">" + obj.getName() + "</option>\r\n");
                }
            });
            html.append("</select>\r\n");
        }
        if (StringUtils.isBlank((CharSequence)theme)) {
            html.append("</div>\r\n</div>\r\n");
        }
        if (ToolUtil.isNotEmpty((Object)underline) && "true".equals(underline)) {
            html.append("<div class=\"hr-line-dashed\" ></div >\r\n");
        }
        try {
            this.ctx.byteWriter.writeString(html.toString());
        }
        catch (IOException e) {
            throw new RuntimeException("\u8f93\u51fa\u5b57\u5178\u6807\u7b7e\u9519\u8bef");
        }
    }
}
